<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { WeilaCore} from 'sdk/main/weila_core'
// import { WeilaAudio, WeilaRecorder, WeilaPlayer, WeilaOpusEncoder } from 'sdk/weila_media'
import axios from 'axios'
import { WeilaH5Network } from 'sdk/main/weila_h5_network';
import { WeilaStorageH5Impl } from 'sdk/main/weila_storage_h5';
import PttButton from './button.vue';
import { WL_SessionInfo } from 'sdk/store/weila_datas';
import Long from 'long';

const w: WeilaCore = new WeilaCore();
setTimeout(() => {
    w.weila_init(new WeilaH5Network(), new WeilaStorageH5Impl());
    console.log('w.weila_init()')
}, 1000);

defineProps<{ msg: string }>()

const onLogin = () => {
  w.weila_login("10000010", "123456", "0").then((res) => {
    console.log("from hello", res)


    w.weila_listenNotification((notification, data) => {
      console.log("listenNotification", notification, data)
    })

    setTimeout(() => {
      w.weila_get_session_info(0).then((res) => {
        console.log("get session info", res)
      })
    }, 2000)
  }).catch((err) => {
    console.log("from hello", err)
    window.alert(err)
  });
}

const onLogout = () => {
  console.log("w.weila_logout()");
  
  w.weila_logout().then((res) => {
    console.log("from hello", res)
  }).catch((err) => {
    console.log("from hello", err)
    window.alert(err)
  });
}


const audioInited = ref(false)
//const weilaAudio = new WeilaAudio();

const audio_init = () => {
  // weilaAudio.weilaAudio_init(16000).then((res) => {
  //   console.log("weilaAudio_init", res)
  //   audioInited.value = true
  // }).catch((err) => {
  //   console.log("weilaAudio_init", err)
  // })
}

const recordText = ref("开始录音")
// let recorder: WeilaRecorder|null;
// let encoder: WeilaOpusEncoder|null;
const record = async () => {
  // if (recordText.value == "开始录音") {
  //   recordText.value = "结束录音"
  //   recorder = weilaAudio.weilaAudio_getRecorder();
  //   encoder = weilaAudio.weilaAudio_getEncoder();

  //   encoder.setCodedDataRecvCallback((data) => {
  //     console.log("setCodedDataRecvCallback", data)
  //   })

  //   recorder.weilaRecorder_setDataReceiveCallback((data: Int16Array) => {
  //     encoder?.encoderData(data);
  //   })

  //   try {
  //     let res = await recorder.weilaRecorder_start();
  //     console.log("weilaRecorder_open", res)
  //     await encoder?.startEncoder(16000, 15000);
  //   }catch (e) {
  //     console.log("weilaRecorder_open", e)
  //   }
  // } else {
  //   recordText.value = "开始录音"
  //   await recorder?.weilaRecorder_stop();
  //   await encoder?.stopEncoder();
  //   recorder = null;
  // }
}

const playText = ref("播放")
// let player: WeilaPlayer|null;

const playBuffer = async () => {
  // if (playText.value == "播放") {
  //   playText.value = "停止"
  //   if (!player) {
  //     player = weilaAudio.weilaAudio_getPlayer(16000, 1);
  //     player.weilaPlayer_setPlayEndListener(async (id: number) => {
  //       await player?.weilaPlayer_stop();
  //       playText.value = "播放"
  //       player = null;
  //     })
  //     await player.weilaPlayer_start()
  //   }
  // } else {

  //   await player?.weilaPlayer_stop();
  //   playText.value = "播放"
  //   player = null;
  // }
}

const feedData = async () => {
  // console.log("feedData");
  // let unit8Array;
  // axios.get(new URL('/audio.ptt', import.meta.url).href, { responseType: 'arraybuffer'}).then((res) => {
  //   console.log("feedData", res);
  //   const data = res.data;
  //   unit8Array = new Uint8Array(data);
  //   console.log("feedData", unit8Array);
  //   if (player) {
  //     player.weilaPlayer_feedWeilaAudioBuffer(unit8Array.slice(6), false);
  //     player.weilaPlayer_feedWeilaAudioBuffer(null, true);
  //   }
  // })
}

const pauseText = ref("暂停")
const pause = async () => {
  // if (pauseText.value == "暂停") {
  //   pauseText.value = "继续"
  //   await player?.weilaPlayer_pause();
  // } else {
  //   pauseText.value = "暂停"
  //   await player?.weilaPlayer_resume();
  // }
}

const ptt_button_handler = (event: any) => {
  console.log("ptt_button_handler", event);
}

const getmsg = () => {
  
  let session: WL_SessionInfo = {} as WL_SessionInfo;
  session.sessionId = new Long(27979);
  session.sessionType = 1;
  w.weila_get_msgs(session, 0, 100).then((res) => {
    console.log("weila_get_msgs", res)
  }).catch((err) => {
    console.log("weila_get_msgs", err)
  })
}
</script>

<template>
  <div style="display: flex; flex-direction: column; gap: 20px;">
    <button @click="onLogin">login</button>
    <button @click="onLogout">logout</button>

    <button @click="audio_init" v-if="!audioInited">Audio Init</button>
    <button @click="record" v-else >{{ recordText }}</button>

    <button @click="playBuffer">{{ playText }}</button>
    <button @click="pause">{{ pauseText }}</button>
    <button @click="feedData">喂数据</button>
    <button @click="getmsg">getmsg</button>

    <PttButton @long-press-start="ptt_button_handler" @release="ptt_button_handler" buttonText="长按录音"></PttButton>
  </div>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
